package com.bytz.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.common.aspect.annotation.AutoLog;
import com.bytz.common.system.api.ISysConfigApi;
import com.bytz.common.system.base.controller.BytzController;
import com.bytz.common.system.query.QueryGenerator;
import com.bytz.common.system.vo.SysConfigVo;
import com.bytz.modules.system.constant.SysAuthConstants;
import com.bytz.modules.system.entity.SysConfig;
import com.bytz.modules.system.model.SysConfigSaveRo;
import com.bytz.modules.system.service.ISysConfigCrudService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/sys/config")
@Api(tags = "系统参数管理")
public class SysConfigController extends BytzController<SysConfig, ISysConfigCrudService> {

    @Autowired
    private ISysConfigApi sysConfigApi;

    @Value("${spring.profiles.active:}")
    private String profile;

    /**
     * 分页列表查询
     */
    @ApiOperation(value="系统参数-分页列表查询", notes="系统参数-分页列表查询")
    @GetMapping(value = "/list")
    @PreAuthorize("hasAuthority('" + SysAuthConstants.SysConfigPermission.CONFIG_VIEW + "')")
    public Result<IPage<SysConfig>> queryPageList(SysConfig sysConfig,
                                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                  HttpServletRequest req) {
        QueryWrapper<SysConfig> queryWrapper = QueryGenerator.initQueryWrapper(sysConfig, req.getParameterMap());
        Page<SysConfig> page = new Page<>(pageNo, pageSize);
        IPage<SysConfig> pageList = service.queryPage(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param sysConfig 系统参数
     * @return
     */
    @AutoLog(value = "系统参数-添加")
    @ApiOperation(value="编码生成器-添加", notes="编码生成器-添加")
    @PreAuthorize("hasAuthority('" + SysAuthConstants.SysConfigPermission.CONFIG_EDIT + "')")
    @PostMapping(value = "/save")
    public Result<?> save(@RequestBody @Valid SysConfigSaveRo sysConfig) {
        service.saveOrUpdateSaveObject(sysConfig, true);
        return Result.ok("保存成功！");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "系统参数-通过id删除")
    @ApiOperation(value="系统参数-通过id删除", notes="系统参数-通过id删除")
    @PreAuthorize("hasAuthority('" + SysAuthConstants.SysConfigPermission.CONFIG_EDIT + "')")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST })
    public Result<?> delete(@RequestParam(name="id") String id) {
        service.checkAndRemoveById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="系统参数-通过id查询", notes="系统参数-通过id查询")
    @PreAuthorize("hasAuthority('" + SysAuthConstants.SysConfigPermission.CONFIG_VIEW + "')")
    @GetMapping(value = "/queryById")
    public Result<SysConfigSaveRo> queryById(@RequestParam(name="id") String id) {
        return Result.ok(service.getSaveVoById(id));
    }

    @ApiOperation(value="系统参数-按Key查询")
    @GetMapping(value = "/getByKey")
    public Result<Object> getByKey(@RequestParam(name="key") String key) {
        return Result.ok(sysConfigApi.getOrDefaultConfig(key, null, Object.class));
    }

    @ApiOperation(value="系统参数-按组查询")
    @GetMapping(value = "/getByGroup")
    public Result<List<SysConfigVo>> getByGroup(@RequestParam(name="group") String group) {
        return Result.ok(sysConfigApi.getByGroup(group));
    }

    @ApiOperation(value="系统参数-查询系统当前的profile")
    @GetMapping(value = "/getProfile")
    public Result<String> getProfile() {
        return Result.ok(profile);
    }
}
